define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'wms/purchasesubsidiary/index' + location.search,
                    add_url: 'wms/purchasesubsidiary/add',
                    edit_url: 'wms/purchasesubsidiary/edit',
                    del_url: 'wms/purchasesubsidiary/del',
                    multi_url: 'wms/purchasesubsidiary/multi',
                    import_url: 'wms/purchasesubsidiary/import',
                    table: 'purchasesubsidiary',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'storegoods.name', title: __('Storegoods.name'), operate: 'LIKE'},
                        {field: 'storegoodslist.name', title: __('Storegoodslist.name'), operate: 'LIKE'},
                        {field: 'num', title: __('Num'), operate: 'LIKE'},
                        {field: 'goodunit', title: __('Goodunit'), operate: 'LIKE'},
                        {field: 'warehouse.name', title: __('Warehouse.name'), operate: 'LIKE'},
                        {field: 'reservoir.name', title: __('Reservoir.name'), operate: 'LIKE'},
                        {field: 'shelves.name', title: __('Shelves.name'), operate: 'LIKE'},
                        {field: 'remark', title: __('Remark'), operate: 'LIKE'},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            buttons:[{
                                name:'purchasedetails',
                                text:'收货记录管理',
                                icon:'fa fa-list',
                                classname:'btn btn-info btn-xs btn-detail btn-dialog',
                                url:function (row){
                                    return 'wms/purchasedetails/index?purchasesubsidiary_id='+row.id;
                                }
                            },{
                                name:'receiving',
                                text:'收货/退货',
                                icon:'fa fa-list',
                                classname:'btn btn-info btn-xs btn-detail btn-dialog',
                                url:function (row){
                                    return 'wms/purchasedetails/add?purchasesubsidiary_id='+row.id+'&purchase_id='+row.purchase_id;
                                }
                            }
                            ],
                            formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'wms/purchasesubsidiary/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'wms/purchasesubsidiary/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'wms/purchasesubsidiary/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
